﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CachingProject
{
    class Meta
    {
        int curAddy;        // the current address being stored
        Cache newCache;     // the new Cache object
        string fileName;    // the path of the file being read
        bool endOfFile;     // whether or not the end of the file has been reached
        StreamReader file;  // the actual file to be read

        // Constructor
        public Meta(string fName, int cSize, int bSize, int assoc)
        {
            curAddy = 0;
            newCache = new Cache(cSize, bSize, assoc);
            fileName = fName;
            endOfFile = false;
            loadFile(fileName);
        }

        // Loads the file specified by the file path into the 'file' variable
        public void loadFile(string path)
        {
            file = new StreamReader(path);
        }

        // Interacts with the Cache object, pushes all addresses from the file into the Cache
        public void DoMeta()
        {
            while (!endOfFile)
            {
                int addy = GetLine();
                if (!endOfFile)
                {
                    newCache.DoCache(addy);
                    newCache.Search(addy);
                }
            }
        }
        
        // returns the number of hits
        public int Hit()
        {
            return newCache.GetHit;
        }
        // returns the number of misses
        public int Miss()
        {
            return newCache.GetMiss;
        }
        // returns the miss rate
        public double MissRate()
        {
            int miss = newCache.GetMiss;
            int hit = newCache.GetHit;
            int total = miss + hit;
            double rate = (double)miss / total;
            return rate;
        }
        // gets the next line of the file and performs all necessary error checking
        private int GetLine()
        {
            string line = "0";
            if (File.Exists(this.fileName))            //If the filepath exists
            {
                try
                {
                    line = file.ReadLine();                        //reads line
                    if (line == null)                    //If a null line found
                    {
                        endOfFile = true;                   //found end of file
                    }
                }
                catch                                      //if file read error
                {
                    Console.WriteLine("Error with file");       //print message
                }
            }
            else                                            //if file not found
            {
                Console.WriteLine("Error: File Not Found");     //Print message
            }
            int temp;
            temp = Convert.ToInt32(line);   // convert the string to an int
            curAddy += temp;                // account for the offsetted file
            return curAddy;                 // return the new address
        }
        // will return true if the end of the file has been reached
        public bool EOF
        {
            get
            {
                return endOfFile;
            }
        }
    }
}